from lib.pre_post_lib import *
import numpy as np


def _pre_post(param, brt):
    # 1.Corner filling
    if param['Process']['corner_filling'] == '1':
        for color_idx in brt.color:
            for gray_idx in range(len(brt.gray_level)):
                brt[color_idx][...,gray_idx] = corner_filling._corner_filling(brt[color_idx][...,gray_idx])
        print('pre_post: corner filling')

    return brt

def _img_crop(img, maps):
    # 选择最大外接矩形
    min_x = np.min(maps[..., 1])
    max_x = np.max(maps[..., 1])
    min_y = np.min(maps[..., 0])
    max_y = np.max(maps[..., 0])

    # overlap
    overlap = 20
    min_x -= overlap
    max_x += overlap
    min_y -= overlap
    max_y += overlap

    # coordinate should be even, used for mono and color camera
    if min_x % 2: min_x -= 1
    if max_x % 2: max_x += 1
    if min_y % 2: min_y -= 1
    if max_y % 2: max_y += 1

    # select ROI
    img_roi = img[min_x:max_x, min_y:max_y]

    # transformer maps
    maps[..., 0] = maps[..., 0] - min_y
    maps[..., 1] = maps[..., 1] - min_x

    return img_roi, maps
